import pygame
import random
import os
from pygame.locals import *
from math import *
from defs import *

class Rabbit(pygame.sprite.Sprite):

   colour = YELLOW
   
   turn_speed = 15
   expanded = 0
   exploded = 0
   max_speed = 4.0
   
   imageCounter = 0
   rabbitDirection = 0
   
   # draw base image
   base_image2 = pygame.Surface((100, 50))
   base_image2.fill(TRANSPARENT)
   base_image2.set_colorkey(TRANSPARENT)
   pointlist2 = [(0,0), (0, 50), (100, 25)]
   pygame.draw.polygon(base_image2, colour, pointlist2)
   base_image3 = pygame.Surface((300, 300))
   base_image3.fill(TRANSPARENT)
   base_image3.set_colorkey(TRANSPARENT)
   pointlist3 = [(0,0), (0, 300), (300, 300), (300, 0)]
   pygame.draw.polygon(base_image3, colour, pointlist3)
   
   try:
      rabbit1 = pygame.image.load(os.path.join('art','rabbitRight00.png'))
      rabbit2 = pygame.image.load(os.path.join('art','rabbitRight01.png'))
      rabbit3 = pygame.image.load(os.path.join('art','rabbitRight00.png'))
      rabbit4 = pygame.image.load(os.path.join('art','rabbitRight02.png'))
      rabbitRightImages = (rabbit1,rabbit2,rabbit3,rabbit4)
      
      rabbit1 = pygame.image.load(os.path.join('art','rabbitLeft00.png'))
      rabbit2 = pygame.image.load(os.path.join('art','rabbitLeft01.png'))
      rabbit3 = pygame.image.load(os.path.join('art','rabbitLeft00.png'))
      rabbit4 = pygame.image.load(os.path.join('art','rabbitLeft02.png'))
      rabbitLeftImages = (rabbit1,rabbit2,rabbit3,rabbit4)
      
      rabbit1 = pygame.image.load(os.path.join('art','rabbitRightLar00.png'))
      rabbit2 = pygame.image.load(os.path.join('art','rabbitRightLar01.png'))
      rabbit3 = pygame.image.load(os.path.join('art','rabbitRightLar00.png'))
      rabbit4 = pygame.image.load(os.path.join('art','rabbitRightLar02.png'))
      rabbitLarRightImages = (rabbit1,rabbit2,rabbit3,rabbit4)
      
   except:
      base_image = pygame.Surface((20, 10))
      base_image.fill(TRANSPARENT)
      base_image.set_colorkey(TRANSPARENT)
      pointlist = [(0,0), (0,10), (20,5)]
      pygame.draw.polygon(base_image, colour, pointlist)
      
   rabbitImages = rabbitRightImages

   def __init__(self):
      pygame.sprite.Sprite.__init__(self)

      try:
         texture = pygame.Surface.convert_alpha(self.rabbitImages[0])
         self.image = texture.copy()
      except:
         self.image = self.base_image.copy()
	
      x = int(SCREENRECT.width * random.random())
      y = int((SCREENRECT.height - FONT_SIZE) * random.random())
      self.rect = self.image.get_rect(center=(x,y))

      self.angle = 0
      a = radians(self.angle)
      self.vx = self.max_speed * cos(a)
      self.vy = self.max_speed * sin(a)

   def update(self):
      # move the sprite
      if (self.exploded == 0):
         try:
            texture = pygame.Surface.convert_alpha(self.rabbitImages[self.imageCounter])
            self.image = pygame.transform.rotate(texture.copy(), -self.angle)
         except:
            self.image = pygame.transform.rotate(self.base_image.copy(), -self.angle)
      else:
         self.image = self.base_image3.copy()
      
      self.rect.move_ip(self.vx, self.vy)
      self.wrap()

   def wrap(self):
      # wrap around the edges of the screen
      if (self.rect.center[0] < 0):
         self.rect.move_ip(SCREENRECT.width, 0)
      elif (self.rect.center[0] >= SCREENRECT.width):
         self.rect.move_ip(-SCREENRECT.width, 0)            

      if (self.rect.center[1] <  FONT_SIZE):
         self.rect.move_ip(0, SCREENRECT.height - FONT_SIZE)
      elif (self.rect.center[1] >= SCREENRECT.height):
         self.rect.move_ip(0, -SCREENRECT.height + FONT_SIZE)
	 
   def expand(self):
      #self.image = pygame.transform.scale(self.base_image.copy(), (100, 50))
      self.rabbitImages = self.rabbitLarRightImages
      try:
         texture = pygame.Surface.convert_alpha(self.rabbitLarRightImages[self.imageCounter])
         self.image = pygame.transform.rotate(texture.copy(), -self.angle)
      except:
         self.image = pygame.transform.rotate(self.base_image2, -self.angle)
      self.rect = self.image.get_rect(center=self.rect.center)
      self.expanded = 1
      
   def deflate(self):
      self.expanded = 0
      try:
         texture = pygame.Surface.convert_alpha(self.rabbitImages[self.imageCounter])
         self.image = pygame.transform.rotate(texture.copy(), -self.angle)
      except:
         self.image = pygame.transform.rotate(self.base_image.copy(), -self.angle)
      
   def explode(self):
      #self.rabbitImages = self.rabbitLarRightImages
      # try:
         # texture = pygame.Surface.convert_alpha(self.rabbitImages[self.imageCounter])
         # self.image = pygame.transform.rotate(texture.copy(), -self.angle)
      # except:
      self.image = self.base_image3.copy()
      self.rect = self.image.get_rect(center=self.rect.center)
      self.exploded = 1

   def implode(self):
      self.exploded = 0
      try:
         texture = pygame.Surface.convert_alpha(self.rabbitImages[self.imageCounter])
         self.image = pygame.transform.rotate(texture.copy(), -self.angle)
      except:
         self.image = pygame.transform.rotate(self.base_image.copy(), -self.angle)
      self.rect = self.image.get_rect(center=self.rect.center)
      
   def changeImageCounter(self):
      if (self.imageCounter == 3):
         self.imageCounter = 0;
      else:
         self.imageCounter += 1

   def turn(self, direction):
      self.rabbitDirection = direction
      self.angle += self.turn_speed * direction
      
      while (self.angle < 0):
         self.angle += 360
      while (self.angle >= 360):
         self.angle -= 360
      a = radians(self.angle)
      self.vx = self.max_speed * cos(a)
      self.vy = self.max_speed * sin(a)
      
      if (((self.angle >= 0) and (self.angle < 90)) or ((self.angle >= 270) and (self.angle < 360))):
         self.rabbitImages = self.rabbitRightImages
      else:
         #self.rabbitImages = self.rabbitLeftImages
         self.rabbitImages = self.rabbitRightImages

      # Note: rotate() takes an angle in degrees
      if (self.expanded == 0 and self.exploded == 0):
         try:
            texture = pygame.Surface.convert_alpha(self.rabbitImages[self.imageCounter])
            self.image = pygame.transform.rotate(texture.copy(), -self.angle)
         except:
            self.image = pygame.transform.rotate(self.base_image, -self.angle)
      elif (self.exploded == 1):
         #self.rabbitImages = self.rabbitLarRightImages
         # try:
            # texture = pygame.Surface.convert_alpha(self.rabbitImages[self.imageCounter])
            # self.image = pygame.transform.rotate(texture.copy(), -self.angle)
         # except:
         self.image = self.base_image3.copy()
      elif (self.expanded == 1):
         self.rabbitImages = self.rabbitLarRightImages
      else:
          self.image = pygame.transform.scale(self.base_image2.copy(), (100, 50))
          self.image = pygame.transform.rotate(self.image, -self.angle)
      self.rect = self.image.get_rect(center=self.rect.center)
